//
//  DataListSelectedView.m
//  PCDBankSix
//
//  Created by APPLE on 2023/8/24.
//

#import "DataListSelectedView.h"

@interface DataListSelectedView () <UIPickerViewDelegate,UIPickerViewDataSource>
@property (strong, nonatomic) UIPickerView *picker;    //选择器
@property (copy, nonatomic) NSString *title;
@property (copy, nonatomic) NSString *selectValue;   //选择的值
@property (strong, nonatomic) NSArray<NSString *> *data;

@end
@implementation DataListSelectedView

 //初始化方法
 - (instancetype)initWithSelectValue:(NSString *)selectValue dataArray:(NSArray *)dataArray{
     if (self = [super init]) {
         _selectValue = selectValue;
         _data = dataArray;

         [self initUI];
     }

     return self;
 }
 
 //UI布局，主要就是在弹出视图上添加选择器
 - (void)initUI{
     [super initUI];
     // 添加选择器
     [self.alertView addSubview:self.picker];
 }
 
 //选择器的初始化和布局
 - (UIPickerView *)picker{
     if (!_picker) {
         _picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, kTopViewHeight + 0.5, SCREEN_WIDTH, kDatePicHeight)];
 //        _picker.backgroundColor = [UIColor whiteColor];
         _picker.showsSelectionIndicator = YES;
         //设置代理
         _picker.delegate =self;
         _picker.dataSource =self;
         for (NSInteger i = 0; i < self.data.count; i++) {
             NSString *string = self.data[i];
             if ([string isEqualToString:_selectValue]) {
                 [_picker selectRow:i inComponent:0 animated:NO];
             }
         }
     }
     return _picker;
 }
 
 #pragma mark - UIPickerView的数据和布局，和tableview类似
 //返回多少列
 -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
     return 1;
 }
 
 //返回多少行
 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
     return self.data.count;
 }
 
 //每一行的数据
 -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
     return self.data[row];
 }
 
 //选中时的效果
 -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
     self.selectValue = self.data[row];
 }
 
 //返回高度
 -(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
     return 35.0f;
 }
 
 //返回宽度
 -(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
     return SCREEN_WIDTH;
 }
 
 #pragma mark - 背景视图的点击事件
 - (void)didTapBackgroundView:(UITapGestureRecognizer *)sender {
     //    [self dismissWithAnimation:NO];
 }
 
 #pragma mark - 弹出视图方法
 - (void)showWithAnimation:(BOOL)animation {
     //1. 获取当前应用的主窗口
     UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
     [keyWindow addSubview:self];
     if (animation) {
         // 动画前初始位置
         CGRect rect = self.alertView.frame;
         rect.origin.y = SCREEN_HEIGHT;
         self.alertView.frame = rect;
         // 浮现动画
         [UIView animateWithDuration:0.3 animations:^{
             CGRect rect = self.alertView.frame;
             rect.origin.y -= kDatePicHeight + kTopViewHeight;
             self.alertView.frame = rect;
         }];
     }
 }
 
 #pragma mark - 关闭视图方法
 - (void)dismissWithAnimation:(BOOL)animation {
     // 关闭动画
     [UIView animateWithDuration:0.2 animations:^{
         CGRect rect = self.alertView.frame;
         rect.origin.y += kDatePicHeight + kTopViewHeight;
         self.alertView.frame = rect;
         
         self.backgroundView.alpha = 0;
     } completion:^(BOOL finished) {
         [self.leftBtn removeFromSuperview];
         [self.rightBtn removeFromSuperview];
         [self.titleLabel removeFromSuperview];
         [self.lineView removeFromSuperview];
         [self.topView removeFromSuperview];
         [self.picker removeFromSuperview];
         [self.alertView removeFromSuperview];
         [self.backgroundView removeFromSuperview];
         [self removeFromSuperview];
         
         self.leftBtn = nil;
         self.rightBtn = nil;
         self.titleLabel = nil;
         self.lineView = nil;
         self.topView = nil;
         self.picker = nil;
         self.alertView = nil;
         self.backgroundView = nil;
     }];
 }
 
 #pragma mark - 取消按钮的点击事件
 - (void)clickLeftBtn {
     [self dismissWithAnimation:YES];
 }
 
 #pragma mark - 确定按钮的点击事件
 - (void)clickRightBtn {
     [self dismissWithAnimation:YES];
     if([self.delegate respondsToSelector:@selector(getDatePickerResult:)]) {
         [self.delegate getDatePickerResult:_selectValue];
     }
 }

@end
